package com.ican.dao.user;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.ican.common.hibernate.HibernateDao;
import com.ican.domain.user.Resources;

@Repository("resourcesDao")
public class ResourcesDao extends HibernateDao<Resources, Integer> {

    public List<Resources> findResourceByUserId(Integer userId){
        String hql = "select r from User u join u.resources r where u.id = ? and u.dataStatus = 0 and r.id > 1 and r.dataStatus = 0 order by r.pid, r.sort";
        return this.find(hql, userId);
    }
    
    public List<Resources> findResourceByRoleId(Integer roleId){
        String hql = "select r from Role rl join rl.resources r where rl.id = ? and rl.dataStatus = 0 and r.id > 1 and r.dataStatus = 0 order by r.pid, r.sort";
        return this.find(hql, roleId);
    }
    
    public List<Resources> findResourceByRoleIds(String roleIds){
        String hql = "select r from Role rl join rl.resources r where rl.id in(" + roleIds + ") and rl.dataStatus = 0 and r.id > 1 and r.dataStatus = 0 order by r.pid, r.sort";
        return this.find(hql);
    }
    
    @SuppressWarnings("unchecked")
    public List<Resources> findResourceByUserIdAndRoleId(Integer userId, String roleIds){
        String sql = "SELECT r.* " +
        		"FROM t_resources r JOIN t_user_resources u ON r.id = u.resources_id AND r.id > 1 AND  r.data_status = 0 AND u.user_id = ? " +
        		"UNION SELECT r.* " +
        		"FROM t_resources r JOIN t_role_resources rr ON r.id = rr.resources_id AND r.id > 1 AND r.data_status = 0 AND rr.role_id in (" +
        		roleIds + ") ORDER BY pid, sort";
        return this.createSqlQuery(sql, userId).addEntity(Resources.class).list();
    }
    
    public Resources findResourceById(Integer id){
        String hql = "from Resources r where r.id = ?";
        return this.findUnique(hql, id);
    }
    
    public List<Resources> findAllResource(int type){
        String hql = null;
        if(type == 0){
            hql = "from Resources r where r.dataStatus = 0 order by r.pid, r.sort";
        }else if(type == 1){
            hql = "from Resources r where r.dataStatus = 1 order by r.pid, r.sort";
        }else{
            hql = "from Resources r order by r.sort";
        }
        return this.find(hql);
    }
    
    public List<Resources> findAllResourceByPid(Integer pid){
        String hql = "from Resources r where r.pid = ? order by r.sort";
        return this.find(hql, pid);
    }
    
    public void updateResourcesNameAndUrl(Resources resources){
        String hql = "update Resources r set r.name = ?, r.url = ?, r.modifyTime = ? where r.id = ?";
        this.batchExecute(hql, new Object[]{resources.getName(), resources.getUrl(), new Date(), resources.getId()});
    }
    
    public void updateResourcesDataStatus(Integer id, Integer dataStatus){
        String hql = "update Resources r set r.dataStatus = ?, r.modifyTime = ? where r.id = ?";
        this.batchExecute(hql, new Object[]{dataStatus, new Date(), id});
    }
    
    public void updateResources(Resources resources){
        String hql = "update Resources r set r.name = ?, r.url = ?, r.dataStatus = ?, r.modifyTime = ? where r.id = ?";
        this.batchExecute(hql, new Object[]{resources.getName(), resources.getUrl(), resources.getDataStatus(), new Date(), resources.getId()});
    }
    
    public void updateResourcesPid(Integer fromId, Integer toId){
        String hql = "update Resources r set r.pid = ?, r.modifyTime = ?, r.sort = 0 where r.id = ?";
        this.batchExecute(hql, new Object[]{toId, new Date(), fromId});
    }
    
    public void updateResourcesUrl(Integer id, String url){
        String hql = "update Resources r set r.url = ? where r.id = ?";
        this.batchExecute(hql, new Object[]{url, id});
    }
    
    public void addResources(Resources resources){
        resources.setCreateTime(new Date());
        this.save(resources);
    }
}
